package com.sq.partner.manager.controller;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.CacheManager;
import com.sq.partner.manager.common.Config;
import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.DictCache;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.common.exception.AppException;
import com.sq.partner.manager.mybatis.model.DictDto;
import com.sq.partner.manager.mybatis.model.FacadeUser;
import com.sq.partner.manager.mybatis.model.GatewayChannelUser;
import com.sq.partner.manager.mybatis.model.GatewayTrans;
import com.sq.partner.manager.service.GetwayChannelUserService;
import com.sq.partner.manager.service.TestService;
import com.sq.partner.manager.util.DateUtil;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SQLReader;
import com.sq.partner.manager.util.StrUtil;

@Controller
public class TestController {
	private static Logger logger = LogUtil.getLog(TestController.class);
	@Resource
	private TestService testService;
	@Resource
	private GetwayChannelUserService userService;
	/**
	 * 页面初始化
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/test")
	public String init(ModelMap model, HttpServletRequest request)
			throws Exception {
		logger.info("测试开始111112");
		System.out.println(SQLReader.getSQL("queryCountUser"));
		System.out.println(Config.getProp("linux.log.path"));
		logger.info("linux.log.path" + Config.getProp("linux.log.path"));
		return "test_success";
	}
	@RequestMapping(value = "/clearAllCache")
	public String clearAllCache()throws Exception {
		CacheManager cacheManager=new CacheManager();
		cacheManager.clearAllCache("");
		return "test_success";
	}
	/**
	 * ajax 测试
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/testAjax")
	public @ResponseBody GatewayChannelUser testAjax(ModelMap model, HttpServletRequest request)
			throws Exception {
		String name = request.getParameter("name");
		if(StrUtil.isEmpty(name)){
			throw new AppException("用户名为空");
		}else{
			GatewayChannelUser user = new GatewayChannelUser();
			user.setUserName(name);
			user.setUserPassword("123456");
			user.setFkChannelCode("sample");
			return user;
		}
	}
	@RequestMapping(value = "/ajaxMessage")
	public @ResponseBody Page ajaxMessage(String data)
			throws Exception {
		Page page=new Page();
		if(null==data||"fail".equals(data)){
			throw new AppException();
		}
		if("success".equals(data)){
			page.setResult("success");
			page.setMessage("测试成功");
			return page;
		}
		if("error".equals(data)){
			page.setResult("error");
			page.setMessage("弹出错误");
			return page;
		}
		if("".equals(data)){
			page.setResult("success");
			return page;
		}
		return page;
		
	}
	/**
	 * ajax 测试 简单返回
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/testAjax2")
	public @ResponseBody Result testAjax2(ModelMap model, HttpServletRequest request)
			throws Exception {
		String name = request.getParameter("name");
		if(StrUtil.isEmpty(name)){
			throw new AppException("用户名为空");
		}else{
			GatewayChannelUser user = new GatewayChannelUser();
			user.setUserName(name);
			user.setUserPassword("123456");
			user.setFkChannelCode("sample");
			return Result.success("操作成功");
		}
	}
	
	/**
	 * 清除session
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/clearSession")
	public @ResponseBody String clearSession(ModelMap model, HttpServletRequest request)
			throws Exception {
		request.getSession().invalidate();
		return "清理成功";
	}
	
	/**
	 * 页面初始化
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/t1")
	public String t1(ModelMap model, HttpServletRequest request)
			throws Exception {
		logger.info("测试开始t1");
		GatewayTrans gt = new GatewayTrans();
		gt.setChannelCode("test");
		gt.setChannelName("测试");
		gt.setChannelComcode("test_com");
		gt.setChannelComname("测试测试");
		gt.setChannelProductcode("test_product");
		gt.setOperator("sankun");
		gt.setAmount(new BigDecimal("100.123"));
		gt.setBusino("busino");
		gt.setTrxdate(new Date());
		gt.setInsertTime(new Date());
		gt.setUpdateTime(new Date());
		testService.addTrans(gt);
		return "test_success";
	}
	
	/**
	 * 页面初始化
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/testDict")
	public String testDict(ModelMap model, HttpServletRequest request)
			throws Exception {
		logger.info("测试开始testDict");
		List<DictDto> l = DictCache.getDict(Constants.DICT_ID_TYPE);
		System.out.println(l.get(0).getCodeName());
		return "test_success";
	}
	
	
	
	/**
	 * 页面初始化
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/t2")
	public String t2(ModelMap model, HttpServletRequest request)
			throws Exception {
		logger.info("测试开始t2");
		GatewayTrans gt = testService.queryTrans(12);
		gt.setChannelComname("test"+ gt.getTransid());
		gt.setChannelProductcode(null);
		testService.updateTrans(gt);
		
		GatewayTrans gt2 = testService.queryTrans(11);
		gt2.setChannelComname(null);
		gt2.setChannelProductcode(null);
		gt2.setChannelName("测试修改");
		gt2.setUpdateTime(new Date());
		testService.updateTransNotNull(gt2);
		return "test_success";
	}
	
	/**
	 * 页面初始化
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/t3")
	public String t3(ModelMap model, HttpServletRequest request)
			throws Exception {
		logger.info("测试开始t3");
		Map m = new HashMap();
		m.put("channelCode","test");
		List<GatewayTrans> l = testService.queryTransByMap(m);
		for(GatewayTrans gt : l){
			System.out.println(gt.getTransid());
		}
		return "test_success";
	}
	
	/**
	 * 页面初始化
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/t4")
	public String t4(ModelMap model, HttpServletRequest request)
			throws Exception {
		logger.info("测试开始t4");
		GatewayTrans gt = testService.queryTrans(22);
		testService.delTrans(gt);
		
		testService.delTrans(23);
		return "test_success";
	}
	
	/**
	 * 页面初始化
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/t5")
	public String t5(ModelMap model, HttpServletRequest request)
			throws Exception {
		logger.info("测试开始t5");
		for(int i = 0 ;i < 100 ;i++){
			testService.deluser(1);
		}
		
		return "test_success";
	}
	
	/**
	 * 页面初始化
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/b1")
	public String testBatch1(ModelMap model,Integer a, HttpServletRequest request)
			throws Exception {
//		if(a == null){a=1000;}
//		if(a<0){a=0;}
//		logger.info("测试开始b1");
//		List l = new ArrayList(200);
//		long flag = System.currentTimeMillis()/1000;
//		String appNo = "sankun" + flag;
//		Timestamp time = new Timestamp(System.currentTimeMillis());
//		for(int i = 0;i < a;i++){
//			FacadeAppInsured fa = new FacadeAppInsured();
//			fa.setAppNo(appNo);
//			//fa.setClientNo(flag+i+"");
//			fa.setClientName("测试"+flag+i);
//			fa.setIdType("12022");
//			fa.setIdNo("123456");
//			fa.setCreatTime(time);
//			l.add(fa);
//		}
//		
//		testService.batchInsert("insertFacadeAppInsured", l);
		return "test_success";
	}

}
